/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import model.LogFile;
import pojo.LiveServers;
import pojo.Provider;

/**
 *
 * @author A7med Kotb
 */
public class LiveServersDao 
{
    private  Connection c;
    public LiveServersDao(Connection c) {
        this.c = c;
    }
    
//method to insert live server data in database    
public boolean insert(LiveServers ls)
    {
        boolean check=false;
        try 
        {
          PreparedStatement ps= c.prepareStatement("insert into liveservers (ip , type , user_name ,password ,port ,description) values ( ? ,? ,? ,? ,? ,?)");
          ps.setString(1, ls.getIp());
          ps.setString(2, ls.getType());
          ps.setString(3, ls.getUserName());
          ps.setString(4, ls.getPassword());
          ps.setInt(5, ls.getPort());
          ps.setString(6, ls.getDescription());
          int i=ps.executeUpdate();
          if (i == 1)
          {
              LogFile.log(" System ", " Successfully Saved Data of the New Live Server with IP ["+ls.getIp()+"] to Database");
              check=true;
          }
          else{
              LogFile.log(" System ", " failed to log Data of the New Live Server with IP ["+ls.getIp()+"]");
              check=false;
          }
        } 
        catch (SQLException ex) 
        {
            check=false;
            LogFile.log(" System ", " encountered the following exception while trying to save data of the new Live Server with IP ["+ls.getIp()+"], and this is the error ["+ex+"]");
            ex.printStackTrace();
        }
        return check;
    }

    //method to update liveserevr
    public boolean update(LiveServers ls)
    {
        boolean check=false;
        try 
        {
            boolean first=false;
            String updatetxt="";
            if(ls.getType()!=null)
            {
                if (first) 
                {
                    updatetxt+=", ";
                }
                first=true;
                updatetxt+="type='"+ls.getType()+"'";
            }
            if(ls.getUserName()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="user_name='"+ls.getUserName()+"'";
            }
            if(ls.getPassword()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="password='"+ls.getPassword()+"'";
            }
            if(ls.getPort()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="port='"+ls.getPort()+"'";
            }
            if(ls.getDescription()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="description='"+ls.getDescription()+"'";
            }
            PreparedStatement ps=c.prepareStatement("update liveservers set "+updatetxt+" where ip=?");
            ps.setString(1, ls.getIp());
            int i=ps.executeUpdate();
            if(i==1)
            {
                check=true;
            }
        } 
        catch (SQLException ex) 
        {
            check=false;
            LogFile.log(" System "," encountered the following Exception while trying to update Live server " +ls.getIp() +" and this is the error "+ex.getMessage());
           ex.printStackTrace();
        }
        return check;
    }
    
    //method to delete live server from database
    public boolean deleteByIp(String ip)
    {
        boolean check=false;
        try 
        {
            PreparedStatement ps=c.prepareStatement("delete from liveservers where ip=?");
            ps.setString(1, ip);
            int i=ps.executeUpdate();
            if(i == 1)
            {
                check=true;
            }
        } 
        catch (SQLException ex) 
        {
            check=false;
            LogFile.log(" System ", " encountered the Following Error while trying to delete data of live Server "+ip + " and this is the error"+ex.getMessage());
            ex.printStackTrace();
        }
        return check;
    }
    

    //get all Live Server data by its IP
    public LiveServers getByIp(String ip){
        try {
            LiveServers ls=new LiveServers();
            PreparedStatement ps= c.prepareStatement("select * from liveservers where ip=?");
            ps.setString(1, ip);
            ResultSet rset= ps.executeQuery(); 
            if(rset.next()){
                ls.setLsId(rset.getInt(1));
                ls.setIp(rset.getString(2));
                ls.setType(rset.getString(3));
                ls.setUserName(rset.getString(4));
                ls.setPassword(rset.getString(5));
                ls.setPort(rset.getInt(6));
                ls.setDescription(rset.getString(7));
                LogFile.log(" System ", " Successffully retreived all data Available for Live Server with IP ["+ip+"]");
            return ls;
            }
            else{
                LogFile.log(" System ", " did not Find any data for Live Server With the IP ["+ip+"]");
                return null;
            }
        } 
        catch (SQLException ex) 
        {
            LogFile.log(" System "," encountered  the Following Exception while trying to get Available data for Live Server With IP ["+ip+"],the Error Message is : ["+ex+"]");
        }
            return null;
    }

    public LiveServers getById(Integer lstId) 
    {
        try 
        {
            LiveServers ls=new LiveServers();
            PreparedStatement ps= c.prepareStatement("select * from liveservers where ls_id=?");
            ps.setInt(1, lstId);
            ResultSet rset= ps.executeQuery(); 
            rset.next();
                ls.setLsId(rset.getInt(1));
                ls.setIp(rset.getString(2));
                ls.setType(rset.getString(3));
                ls.setUserName(rset.getString(4));
                ls.setPassword(rset.getString(5));
                ls.setPort(rset.getInt(6));
                ls.setDescription(rset.getString(7));
            return ls;
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
            return null;
    }
    
    public Vector<LiveServers> getall()
    {
        Vector<LiveServers> servers=new Vector<LiveServers>();
        try 
        {  
            PreparedStatement ps=c.prepareStatement("select * from liveservers");
            ResultSet result= ps.executeQuery();
            while (result.next()) 
            {
                LiveServers ls=new LiveServers();
                System.out.println("sccccccccccccccccccc"+result.getString(2));
                ls.setLsId(result.getInt(1));
                ls.setIp(result.getString(2));
                ls.setType(result.getString(3));
                if(result.getString(4)!=null)
                ls.setUserName(result.getString(4));
                if (result.getString(5)!=null)
                ls.setPassword(result.getString(5));
                if (result.getString(6)!=null)
                ls.setPort(result.getInt(6));
                if (result.getString(7)!=null)
                ls.setDescription(result.getString(7));
                servers.add(ls);
            }
          
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
        return servers;
        
    }
//get All Live Servers That Match Pattern
    public Vector<LiveServers> getByPattern(String searchtext) 
    {
        Vector<LiveServers> servers=new Vector<LiveServers>();
        try 
        {  
            PreparedStatement ps=c.prepareStatement("select * from liveservers where ip like ?");
            ps.setString(1, searchtext + "%");
            ResultSet result= ps.executeQuery();
            while (result.next()) 
            {
                LiveServers ls=new LiveServers();
                ls.setLsId(result.getInt(1));
                ls.setIp(result.getString(2));
                ls.setType(result.getString(3));
                if(result.getString(4)!=null)
                ls.setUserName(result.getString(4));
                if (result.getString(5)!=null)
                ls.setPassword(result.getString(5));
                if (result.getString(6)!=null)
                ls.setPort(result.getInt(6));
                if (result.getString(7)!=null)
                ls.setDescription(result.getString(7));
                servers.add(ls);
            }
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
        return servers;
    }
}
